package com.sum.tea.ui

import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.annotation.StringRes
import com.blankj.utilcode.util.ToastUtils
import com.sum.tea.base.BaseMvvmFragment
import com.sum.common.bean.ReaderExt.Region_Conf
import com.sum.tea.R
import com.sum.tea.databinding.ActivitySettingBinding
import com.sum.tea.viewmodel.SettingViewModel

class SettingFragment : BaseMvvmFragment<ActivitySettingBinding, SettingViewModel>() {

    private val frequencyAdapter by lazy {
        ArrayAdapter(
            requireContext(),
            android.R.layout.simple_spinner_item,
            resources.getStringArray(R.array.spireg)
        ).apply {
            setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        }
    }

    private val powerAdapter by lazy {
        ArrayAdapter(
            requireContext(),
            android.R.layout.simple_spinner_item,
            (1..33).map { it.toString() }
        ).apply {
            setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        }
    }

    private val protocolAdapter by lazy {
        ArrayAdapter(
            requireContext(),
            android.R.layout.simple_spinner_item,
            listOf("Gen2", "6B", "IPX64", "IPX256")
        ).apply {
            setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        }
    }

    private val qValueAdapter by lazy {
        ArrayAdapter(
            requireContext(),
            android.R.layout.simple_spinner_item,
            listOf("Auto") + (0..15).map { it.toString() }
        ).apply {
            setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        }
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        initSpinners()
        initClickListeners()
        observeViewModel()
    }

    private fun initSpinners() {
        mBinding.snFrequency.adapter = frequencyAdapter
        mBinding.snFrequency.setSelection(1)

        // 功率选择器初始化
        mBinding.snPower.adapter = powerAdapter

        // 协议选择器初始化
        mBinding.snProtocol.adapter = protocolAdapter

        // Q值选择器初始化
        mBinding.snQvalue.adapter = qValueAdapter
    }

    private fun initClickListeners() {
        with(mBinding){
            // 频率相关
            this.buttonGetreg.setOnClickListener {
                val region = mViewModel.getRegion()
                ToastUtils.showLong("Current region: $region")
                val position = frequencyAdapter.getPosition(region)
                if(position!=-1){
                    snFrequency.setSelection(position)
                }
            }
            this.buttonSetreg.setOnClickListener {
                mViewModel.setRegion(snFrequency.selectedItemPosition)
            }

            // 功率相关
            buttonAntpowget.setOnClickListener {
                val power = mViewModel.getPower()
                if(power!=-1){
                    snPower.setSelection(power)
                }
            }
            buttonAntpowset.setOnClickListener {
                mViewModel.setPower(snPower.selectedItemPosition)
            }

            // 协议相关
            buttonInvproget.setOnClickListener {
                val protocol = mViewModel.getProtocol()
                ToastUtils.showShort(protocol)
                val position = protocolAdapter.getPosition(protocol)
                if(position!=-1){
                    snProtocol.setSelection(position)
                }
            }
            buttonInvproset.setOnClickListener {
                mViewModel.setProtocol(snProtocol.selectedItem.toString())
            }

            // Q值相关
            buttonQvalueget.setOnClickListener {
                val qValue = mViewModel.getQValue()
                if(qValue!=-1){
                    snQvalue.setSelection(qValue+1)
                }
            }
            buttonQvalueset.setOnClickListener {
                mViewModel.setQValue(snQvalue.selectedItemPosition)
            }

            buttonSessionFlagGet.setOnClickListener { mViewModel.getSessionAndFlag() }
        }
    }

    private fun observeViewModel() {
        // Session信息观察
        mViewModel.sessionInfo.observe(viewLifecycleOwner) { info ->
            with(mBinding) {
                etSession.setText(info.session)
                etFlag.setText(info.flag)
            }
        }
    }

    private fun updateRegionSpinner(region: Region_Conf?) {
        mBinding.snFrequency.setSelection(when (region) {
            Region_Conf.RG_PRC -> 0
            Region_Conf.RG_NA -> 1
            Region_Conf.RG_NONE -> 2
            Region_Conf.RG_KR -> 7
            Region_Conf.RG_EU -> 4
            Region_Conf.RG_EU2 -> 5
            Region_Conf.RG_EU3 -> 6
            else -> 0
        })
    }
}
